package com.jgzx.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jackesy
 * @className: CityUtil
 * @description: 城市工具类，使用腾讯地图api
 * @create 2020/9/17 21:03
 **/
@Slf4j
public class CityUtil {

    private static final String url = "https://apis.map.qq.com/ws/district/v1/getchildren";
    private static final String key = "OGXBZ-PJUL6-WXHSG-EOG4S-NLNKJ-OKFBQ";
    private static Map<String,String> top_city = new HashMap();//存放顶级城市列表

    static {
        top_city.put("110000", "北京市");
        top_city.put("120000", "天津市");
        top_city.put("130000", "河北省");
        top_city.put("140000", "山西省");
        top_city.put("150000", "内蒙古自治区");
        top_city.put("210000", "辽宁省");
        top_city.put("220000", "吉林省");
        top_city.put("230000", "黑龙江省");
        top_city.put("310000", "上海市");
        top_city.put("320000", "江苏省");
        top_city.put("330000", "浙江省");
        top_city.put("340000", "安徽省");
        top_city.put("350000", "福建省");
        top_city.put("360000", "江西省");
        top_city.put("370000", "山东省");
        top_city.put("410000", "河南省");
        top_city.put("420000", "湖北省");
        top_city.put("430000", "湖南省");
        top_city.put("440000", "广东省");
        top_city.put("450000", "广西壮族自治区");
        top_city.put("460000", "海南省");
        top_city.put("500000", "重庆市");
        top_city.put("510000", "四川省");
        top_city.put("520000", "贵州省");
        top_city.put("530000", "云南省");
        top_city.put("540000", "西藏自治区");
        top_city.put("610000", "陕西省");
        top_city.put("620000", "甘肃省");
        top_city.put("630000", "青海省");
        top_city.put("640000", "宁夏回族自治区");
        top_city.put("650000", "新疆维吾尔自治区");
        top_city.put("710000", "台湾省");
        top_city.put("810000", "香港特别行政区");
        top_city.put("820000", "澳门特别行政区");
    }

    /**
     * 查询下级城市列表
     *
     * @param city_id
     * @return
     */
    public static List<Map<String, Object>> getChildCityList(String city_id) {
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> cityList = new ArrayList<>();
        Map<String, Object> param = new HashMap<>();
        param.put("key", key);
        param.put("id", city_id);
        String res = HttpUtil.get(url, param);
        if (StrUtil.isNotEmpty(res)) {
            JSONObject jsonObject = JSONUtil.parseObj(res);
            if (jsonObject.getInt("status") == 0) {
                JSONArray array = jsonObject.getJSONArray("result");
                if (array != null && array.size() > 0) {
                    JSONArray jsonArray = (JSONArray) array.get(0);
                    if (jsonArray != null && jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject object = jsonArray.getJSONObject(i);
                            Map<String, Object> map = new HashMap();
                            map.put("id", object.getStr("id"));
                            if (StrUtil.isNotEmpty(object.getStr("name"))) {
                                map.put("name", object.getStr("name"));
                            } else {
                                map.put("name", object.getStr("fullname"));
                            }
                            map.put("pid", city_id);
                            cityList.add(map);
                        }
                    }
                }
            }
        }
        return cityList;
    }

    /**
     * 获取全国所有城市
     *
     * @return
     */
    public static List<Map<String, Object>> getAllCityList() {
        List<Map<String, Object>> cityList = new ArrayList<>();
        //一级
        top_city.forEach((key, value) -> {
            Map child1 = new HashMap();
            child1.put("id", key.toString());
            child1.put("name", value.toString());
            child1.put("pid", "100000");//全国
            String path_name1 = value.toString();
            child1.put("path_name", path_name1);
            //二级
            List<Map<String, Object>> children2 = getChildCityList(key.toString());
            if (CollUtil.isNotEmpty(children2) && children2.size() > 0) {
                //三级
                children2.forEach(child2 -> {
                    String path_name2 = path_name1 + " " + child2.get("name");
                    child2.put("path_name", path_name2);
                    List<Map<String, Object>> children3 = getChildCityList((String) child2.get("id"));
                    if (CollUtil.isNotEmpty(children3) && children3.size() > 0) {
                        //四级
                        children3.forEach(child3 -> {
                            String path_name3 = path_name2 + " " + child3.get("name");
                            child3.put("path_name", path_name3);
                            List<Map<String, Object>> children4 = getChildCityList((String) child3.get("id"));
                            if (CollUtil.isNotEmpty(children4) && children4.size() > 0) {
                                children4.forEach(child4 -> {
                                    String path_name4 = path_name3 + " " + child4.get("name");
                                    child4.put("path_name", path_name4);
                                });
                                child3.put("children", children4);
                            }
                        });
                        child2.put("children", children3);
                    }
                });
                child1.put("children", children2);
            }
            cityList.add(child1);
        });
        return cityList;
    }

    public static void main(String[] args) {
        //log.info(JSONUtil.toJsonStr(getChildCityList("110000")));
        log.info(JSONUtil.toJsonStr(getAllCityList()));
    }
}
